
function safe_tags( str ) {
	return String( str )
	.replace( /&/g, '&amp;' )
	.replace( /"/g, '&quot;' )
	.replace( /'/g, '&#39;' )
	.replace( /</g, '&lt;' )
	.replace( />/g, '&gt;' );
}


function txt(mthis){
	var me = $(mthis);
	var str= me.attr("allowfile");
	var max_size= me.attr("max_size");
	if(typeof max_size === "undefined")
	{
		max_size = 1048576;
	}
	var btn = me.attr("id"),
	allowfile = str.split(','),
	mname = me.attr("name"),
	wrap = document.getElementById('pic-progress-wrap-'+btn),
	picBox = document.getElementById('picbox-'+btn),
	errBox = document.getElementById('errormsg-'+btn);
	var uploader = new ss.SimpleUpload({
		button: btn,
		url: url_upload,
		progressUrl: admin_url+'uindex/upload_process',
		name: mname,
		multiple: true,
		maxUploads: 1,
		maxSize: max_size,
		allowedExtensions: allowfile,
		accept: '*',
		hoverClass: 'btn-hover',
		focusClass: 'active',
		disabledClass: 'disabled',
		responseType: 'json',
		onExtError: function(filename, extension) {
			alert(filename + ' is not a permitted file type.'+"\n\n"+'Only '+extension+' files are allowed.');
		},
		onSizeError: function(filename, fileSize) {
			alert(filename + ' is too big. ('+bytesToSize(max_size)+' max file size)');
		},        
		onSubmit: function(filename, ext) {            
			var prog = document.createElement('div'),
			outer = document.createElement('div'),
			bar = document.createElement('div'),
			size = document.createElement('div');
			
			prog.className = 'prog';
			size.className = 'size';
			outer.className = 'progress progress-striped active';
			bar.className = 'progress-bar progress-bar-success';
			
			outer.appendChild(bar);
			//prog.innerHTML = '<span style="vertical-align:middle;">'+safe_tags(filename)+' - </span>';
			//prog.appendChild(size);
			prog.appendChild(outer);
			wrap.appendChild(prog); // 'wrap' is an element on the page
			
			this.setProgressBar(bar);
			this.setProgressContainer(prog);
			this.setFileSizeBox(size);      
			
			errBox.innerHTML = '';
			btn.value = 'Choose another file';
		},		
		startXHR: function() {
			// Dynamically add a "Cancel" button to be displayed when upload begins
			// By doing it here ensures that it will only be added in browses which 
			// support cancelling uploads
			var abort = document.createElement('button');
			
			wrap.appendChild(abort);
			abort.className = 'btn btn-sm btn-info';
			abort.innerHTML = 'Cancel';
			
			// Adds click event listener that will cancel the upload
			// The second argument is whether the button should be removed after the upload
			// true = yes, remove abort button after upload
			// false/default = do not remove
			this.setAbortBtn(abort, true);   
		
		},          
		onComplete: function(filename, response) {
			if (!response) {
				errBox.innerHTML = 'Unable to upload file';
				return;
			}     
			if (response.success === true) {
				//picBox.innerHTML = '<div class="alert alert-success"><button class="close" data-dismiss="alert">×</button><strong>Success! </strong> The file upload success.</div>';
				
				$.notify('Upload thành công.','success');	
				
				$(me).siblings(".btn-image-upload").attr("src",response.file);
				$(me).siblings(".btn-input-upload").val(response.file);
				
			} else {
				if (response.msg)  {
					errBox.innerHTML = '<div class="alert alert-danger alert-dismissable"> <button type="button" data-dismiss="alert" aria-hidden="true" class="close">×</button>'+response.msg+'.</div>';
					
				} else {
					errBox.innerHTML = 'Unable to upload file';
				}
			}
		}
	});
	//});
};

function bytesToSize(bytes) {
    var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
    if (bytes == 0) return 'n/a';
    var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
    if (i == 0) return bytes + ' ' + sizes[i]; 
    return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
};